$(function () {

    /**
     * 初始化图片上传
     */
    uploadImgFile({
        selectObj:'#qa-image-upload-box',
        imgNum:4,
        param:'qaImg',
        previewCallback:function (file,src) {//图片选择后预览回调
            var context = '<li><aside class="qu-picture-remove" data-id="'+file.id+'"><a title="删除" class="fs16 c-ccc" href="javascript:void(0);"><i class="fa fa-close"></i></a></aside>';
            context+='<img style="height: 80px;width: 145px;" class="img-rounded" src="'+src+'"></li>';
            $(".question-upload-picture").append(context);
            $("#def-question-img").parent().remove();
        },
        uploadFinished:function (urlList) {//上传完成回调
            if(urlList.length >0){
                var _imageStr = $("#imageStr").val();
                $.each(urlList,function (i, e) {
                    if(_imageStr == ''){
                        _imageStr += e;
                    }else {
                        _imageStr += ',' + e;
                    }
                });
                $("#imageStr").val(_imageStr);
            }
            saveFeedBack();
        }
    });

    //删除图片
    $("body").on('click',".qu-picture-remove",function () {//删除图片
        var _this = $(this);
        var fileId = _this.data("id");
        deleteFile(fileId,function () {
            _this.parent().remove();
            var lis = $(".question-upload-picture>li");
            if(lis.length<=0){
                var context = '<li><img style="width: 145px;height: 80px;" id="def-question-img" src="'+staticPath+'/front/web/v3/img/default/default-course.gif" class="img-rounded"></li>';
                $(".question-upload-picture").html(context);
            }
        });
    }).on("click","#subjectFeedBack",function(){
        var files = uploader.getFiles();
        if (files && files.length > 0) {
            uploader.upload();
        } else {
            saveFeedBack();
        }
    });

    var actionStatus = true;
    /**
     * 保存反馈数据
     */
    function saveFeedBack() {
      var context =  $('textarea[name="context"]').val();
        if(context.length<=0){
            icModale(0,'','请填写反馈内容');
            return
        }
      if(context.length>225){
          icModale(0,'','反馈内容字数不得超过225');
          $('textarea[name="context"]').val(context.substring(0,225));
          return
      }
        var params = $("#feedBackForm").serializeJSON();
        params = JSON.stringify(params);
         $.ajax({
             url: baseloaction +'/web/feedback/saveUpdateFeedBack',
             type:'post',
             dataType:'json',
             data:params,
             contentType: 'application/json;charset=utf-8',
             success:function (result) {
                 icModale(0,'',result.message);
                 if(result.success==true){
                     var context = '<li><img style="width: 145px;height: 80px;" id="def-question-img" src="'+staticPath+'/front/web/v3/img/default/default-course.gif" class="img-rounded"></li>';
                     $(".question-upload-picture").html(context);
                     $(".fee-img-li-box").remove();
                     $("[name='context']").val("");
                     $("[name='feedBackEmail']").val("");
                     $("#imageUrls").val("");
                     uploader.reset();
                     actionStatus = true;
                     $("#upload-feedback-img").show();
                 }
             },
             error:function (error) {
                 alert("系统错误，意见反馈失败")
             }
         });
    }
});